package com.business.api;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.xserver.common.util.StringUtil;
import org.xserver.component.annotation.Path;
import org.xserver.component.exception.AbstractBussinessException;
import org.xserver.component.prototype.annotation.ReturnType;

import com.business.bean.DefaultJsonResponse;
import com.business.service.AccessTokenService;
import com.business.service.UserService;

@Component
@Path("sampleApi")
public class SampleApi {
	private static final Logger log = LoggerFactory.getLogger(AccessTokenService.class);
	@Resource
	private UserService userService;
	
	@Path(value="login",description="" )
	@ReturnType
	public Object login(String userId,String password){
		DefaultJsonResponse<Object> response = new DefaultJsonResponse<Object>();
		try {
			if(StringUtil.isEmpty(userId)||StringUtil.isEmpty(password)){
				response.setCode(Constant.INVALID_PARAMA_ERROR+"");
				response.setMsg(Constant.INVALID_PARAMA_ERROR_MSG);
				return response;
			}
			response.setCode(Constant.SUCCESS_CODE+"");
			response.setMsg(Constant.SUCCESS_CODE_MSG);
			response.setResult(userService.getAccessToken(userId));
			return response;
		} catch (AbstractBussinessException e) {
			log.error("SampleApi login error",e);
			response.setCode(Constant.SERVER_ERROR+"");
			response.setMsg(Constant.SERVER_ERROR_MSG);
			return response;
		}
		
	}
	@Path(value="trade",description="" )
	@ReturnType
	public Object trade(String userId,String price){
		DefaultJsonResponse<Object> response = new DefaultJsonResponse<Object>();
		try {
			if(StringUtil.isEmpty(userId)||StringUtil.isEmpty(price)){
				response.setCode(Constant.INVALID_PARAMA_ERROR+"");
				response.setMsg(Constant.INVALID_PARAMA_ERROR_MSG);
				return response;
			}
			response.setCode(Constant.SUCCESS_CODE+"");
			response.setMsg(Constant.SUCCESS_CODE_MSG);
			response.setResult(userService.getAccessToken(userId));
			return response;
		} catch (AbstractBussinessException e) {
			response.setCode(Constant.SERVER_ERROR+"");
			response.setMsg(Constant.SERVER_ERROR_MSG);
			return response;
		}
		
	}
	
}
